unit main;

{$mode ObjFPC}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
  ComCtrls, Menus, RTTICtrls, DevToysPlugin, JvOutlookBar,
  BGRASVGImageList, RandomGenerate, uuid_frame;

type

  { TMainForm }

  TMainForm = class(TForm)
    BGRASVGImageList1: TBGRASVGImageList;
    LargeBGRASVGImageList: TBGRASVGImageList;
    SmallBGRASVGImageList: TBGRASVGImageList;
    ImageList1: TImageList;
    SmallImageList: TImageList;
    LargeImageList: TImageList;
    JvOutlookBar1: TJvOutlookBar;
    MainMenu: TMainMenu;
    MenuItemConfig: TMenuItem;
    MenuItemHelp: TMenuItem;
    PanelClient: TPanel;
    Splitter1: TSplitter;
    StatusBar1: TStatusBar;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure JvOutlookBarPagesButtonsClick(Sender: TObject);

  private
    Plugins: TInterfaceList;
    ActivePanel: TControl;

    procedure RegisterPlugin(const p: IDevToysPlugin);
    procedure SwitchPlugin(Index: Integer);
  public

  end;

var
  MainForm: TMainForm;
  FChildFrame: TFrame = nil;

implementation

{$R *.lfm}

{ TMainForm }


procedure TMainForm.FormCreate(Sender: TObject);
var
  i, j: Integer;
  stemp: IDevToysPlugin;
begin
  //Plugins := TInterfaceList.Create;
  // 加载所有插件
  //RandomGenerate.RegisterPlugin(@RegisterPlugin);

  for i := 0 to JvOutlookBar1.Pages.Count - 1 do
  begin
    if JvOutlookBar1.Pages.Items[i].Buttons.Count <= 0 then
       continue;

    for j := 0 to JvOutlookBar1.Pages.Items[i].Buttons.Count - 1 do
    begin
      JvOutlookBar1.Pages.Items[i].Buttons.Items[j].OnClick := @JvOutlookBarPagesButtonsClick;
    end;
  end;

  //加载到JvOutlookBar中
  //try
  //  TreeViewTools.Items.Clear;
  //  for i:= 0 to Plugins.Count - 1 do
  //  begin
  //    temp := Plugins.Items[i] as IDevToysPlugin;
  //    Root := TreeViewTools.Items.Add(nil, temp.Title);
  //    Root.SelectedIndex := 0;
  //    Root.ImageIndex := 0;
  //
  //    Child := TreeViewTools.Items.AddChild(Root, temp.Description);
  //    Child.ImageIndex := 1;
  //    Child.SelectedIndex := 1;
  //    Child.Data := nil;
  //  end;
  //finally
  //
  //end;

end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
  FreeAndNil(Plugins);
end;


procedure TMainForm.JvOutlookBarPagesButtonsClick(Sender: TObject);
var
  FText: string;
begin
  FText := (Sender as TJvOutlookBarButton).Caption;
  StatusBar1.Panels.Items[0].Text := FText;
  StatusBar1.Panels.Items[1].Text := (Sender as TJvOutlookBarButton).GetNamePath();


  if Assigned(FChildFrame) then
  begin
    if FText = FChildFrame.Caption then
    begin
      exit;
    end;
    ShowMessage('释放' + FChildFrame.Caption);
    FChildFrame.Parent := nil;
    FreeAndNil(FChildFrame);
  end;

  if FText = 'UUID生成器' then
  begin
    FChildFrame := TFrameUUID.Create(Application);
    FChildFrame.Parent := PanelClient;
    FChildFrame.Align := alClient;
    FChildFrame.Caption := FText;
  end;



end;


procedure TMainForm.RegisterPlugin(const p: IDevToysPlugin);
begin
   Plugins.Add(p);
end;

procedure TMainForm.SwitchPlugin(Index: integer);
var
  P: IDevToysPlugin;
begin
  if (Index < 0) or (Index >= Plugins.Count) then Exit;
  if Assigned(ActivePanel) then
  begin
    ActivePanel.Visible := False;
    ActivePanel.Parent := nil; // 脱离
  end;
  P := Plugins[Index] as IDevToysPlugin;
  Caption := 'DevToysLaz - ' + P.Title;
  P.BuildUI(PanelClient);
  ActivePanel := PanelClient.Controls[PanelClient.ControlCount - 1];
end;



end.
